var md5 = require("md5");
import store from "../../store/index";
var getSign = function(params) {
  if (typeof params == "string") {
    return paramsStrSort(params);
  } else if (typeof params == "object") {
    var arr = [];
    for (var i in params) {
      arr.push(i + "=" + params[i]);
    }
    return paramsStrSort(arr.join("&"));
  }
};
var paramsStrSort = function(str) {
  // const admin_key = store.state.admin_key || "";
  const admin_key = sessionStorage.getItem("admin_key") || "";
  const uid = sessionStorage.getItem("uid") || "";
  var time = parseInt(new Date().getTime() / 1000);
  store.commit("setTimestamp", time);
  var url = "";
  if (str == "") {
    url =
      str +
      "admin_key=" +
      admin_key +
      "&uid=" +
      uid +
      "&timestamp=" +
      time +
      "&meid=" +
      "" +
      "&extra=" +
      "";
  } else {
    url =
      str +
      "&admin_key=" +
      admin_key +
      "&uid=" +
      uid +
      "&timestamp=" +
      time +
      "&meid=" +
      "" +
      "&extra=" +
      "";
  }

  var urlStr = url
    .split("&")
    .sort()
    .join("&");
  return md5(urlStr);
};

const getUpData = function(data) {
  var da = {};
  var sign = getSign(data);
  data.sign = sign;
  return da;
};

export default getUpData;
